import {UserEquipmentDTO} from './deviceApi';
import {http} from '../http';
import {PagingModel} from '../models/BaseModel';

export type StationStatus = 'normal' | 'offline' | 'pending' | 'installed';

// 电站相关接口类型定义
export interface StationDTO {
  id?: number;
  stationName: string;
  address: string;
  latitude: number;
  longitude: number;
  coverImage: string;
  detailAddress: string;
  timezone: string;
  installerCode: string | undefined;
  stationType: string;
  stationStatus?: StationStatus;
  lastUpdateTime?: string;
  totalPowerGen?: string;
  deviceDTOList?: UserEquipmentDTO[];
  defaultCreate?: 0 | 1;
}
export interface StationEnergyDiagramModel {
  stationStatus: StationStatus;
  nodeCode: string;
  nodeName: string;
  havMeter: boolean;
  lastUpdateTime: null;
  hidden: boolean;
  todayPowerGen: string | null;
  todayUseGen: string | null;
  todayChargeGen: string | null;
  todayGridGen: string | null;
  totalPowerGen: string | null;
  totalUseGen: string | null;
  totalChargeGen: string | null;
  totalGridGen: string | null;
  saveStaCoal: string | null;
  carDioRed: string | null;
  equTreePlanQua: string | null;
  dtuWorkMode: boolean;
  invertEnable: boolean;
  smartPlug: boolean;
  smartMeter: boolean;
  otherNode: StationDiagramOtherNode | null;
  todayData: Record<string, string> | null;
}

export interface StationDiagramOtherNode {
  bat: StationEnergyChildNode | undefined;
  load: StationEnergyChildNode | undefined;
  pv: StationEnergyChildNode | undefined;
  grid: StationEnergyChildNode | undefined;
}

export interface StationEnergyChildNode {
  nodeCode: string;
  nodeName: string;
  // hidden: boolean;
  direction: number;
  batteryPower?: string;
  gridPower?: string;
  loadPower?: string;
  pvPower?: string;
}

export interface StationUpdateDTO {
  name?: string;
  address?: string;
  latitude?: number;
  longitude?: number;
}

export interface StationStatisticsDTO {
  totalPower: number;
  totalEnergy: number;
  totalIncome: number;
  deviceCount: number;
  onlineCount: number;
  offlineCount: number;
  alertCount: number;
}

export interface StationTypeModel {
  id: string;
  dictId: string;
  label: string;
  dictType: string;
  description: string;
  sortOrder: number;
  createBy: string;
  updateBy: string;
  createTime: Date;
  updateTime: null;
  remarks: string;
  delFlag: string;
  value: string;
}

export interface StationInstallerModel {
  instId: string;
  name: string;
  code: string;
  prtId: null;
  county: null;
}

export interface StationInverter {
  deviceSn: string;
  deviceName: string;
  shutdown: number;
  ratePower: number; // 额定功率
  ratePowerFactor: number;
  ratePowerReal: number; // 实际功率
  singleStrength: number; // 信号强度
  deviceStatus: string; // 设备状态
}

export interface InstallerSceneOverviewModel {
  stationCount: number;
  toConfigureStationCount: number;
  toInstallStationCount: number;
  deviceCount: number;
  onlineDeviceCount: number;
  offlineDeviceCount: number;
  normalDeviceCount: number;
  faultDeviceCount: number;
  installTrendList: {
    time: string;
    installCount: number;
  }[];
}
export type StationAlarmModel = {
  level: number;
  viewType: number;
  code: string;
  message: string;
  ct: string;
  deviceSn: string;
  deviceName: string;
};
// API 接口定义
export const stationApi = {
  // 创建电站
  createStation: (data: StationDTO) => {
    return http.post<StationDTO>('biz/v1/biz/station/add', data);
  },

  // 获取电站详情
  getStation: (id: number) => {
    return http.get<StationDTO>(`/v1/biz/station/${id}`);
  },

  // 分页查询用户的所有电站
  getUserStations: (params?: {
    pageSize?: number;
    pageNum?: number;
    stationStatus?: string;
    online?: string;
    stationName?: string;
  }) => {
    return http.get<PagingModel<StationDTO>>('/biz/v1/biz/station', params);
  },

  // 更新电站信息
  updateStation: (station: StationDTO) => {
    return http.post<StationDTO>(`/biz/v1/biz/station/update`, station);
  },

  // 删除电站
  deleteStation: (id: number | string) => {
    return http.delete<boolean>(`/biz/v1/biz/station/${id}`);
  },

  // 获取电站统计数据
  getStationStatistics: (id: number) => {
    return http.get<StationStatisticsDTO>(`/v1/biz/stations/${id}/statistics`);
  },
  // 安装商场景概览
  getInstallerSceneOverview: (params: {
    grain: number;
    year?: number;
    month?: number;
    day?: number;
  }) => {
    return http.get<InstallerSceneOverviewModel>(`/biz/v1/view`, params);
  },

  getStationTypes: () => {
    return http.get<PagingModel<StationTypeModel>>(`admin/dict/item/page`, {
      dictType: 'grid.connected.type',
    });
  },

  getStationInstallers: () => {
    return http.get<StationInstallerModel[]>(`/biz/v1/biz/inst/code`, null, {
      skipAuth: true,
    });
  },

  getEnergyDiagramInfo: (stationId: number | string) => {
    return http.get<StationEnergyDiagramModel>(
      `/biz/v1/biz/station/flow/diagram/${stationId}`,
    );
  },

  getStationInverterSettings: (stationId: number) => {
    return http.get<StationInverter[]>(
      `/biz/v1/biz/device/inverter/read/by/stationId?stationId=${stationId}`,
    );
  },

  updateStationInverter: (data: Partial<StationInverter>) => {
    return http.post<StationInverter>(`biz/v1/biz/device/inverter/set`, data);
  },

  speedUpStationDataRate: (stationId: number | string) => {
    return http.post<boolean>(`biz/v1/biz/station/speed/${stationId}`);
  },
  getStationAlarmList: (params: {
    stationId: number;
    pageNum: number;
    pageSize: number;
  }) => {
    return http.get<PagingModel<StationAlarmModel>>(
      `/biz/v1/biz/station/alarm/list`,
      params,
    );
  },
};
